package neuron.library.ga.common;

import java.util.ArrayList;
import java.util.Collections;

import neuron.library.ga.AbstractGeneticMachine;

/*
 * TODO: Comment!!
 */
public class Population extends ArrayList<AbstractChromosome>{
	
	/*
	 * Instance of AbstractGeneticMachine
	 */
	AbstractGeneticMachine gm;
	
	public Population(AbstractGeneticMachine gm) {
		this.gm = gm;
	}
	
	/*
	 * Calculates fitness for all chromosomes
	 * and sorts population.
	 */
	public void sortPopulation(){
		calculateFitness();
		Collections.sort(gm.getPopulation());
	}
	
	/*
	 * Calculates fitness scores for all population.
	 */
	public void calculateFitness() {
		AbstractFitness abstractFitness = gm.getConfig().getFitness();
		for (AbstractChromosome chromosome: gm.getPopulation()) {
			chromosome.setScore(abstractFitness.fitness(chromosome));
		}
	}
}
